﻿package product;

import java.awt.EventQueue;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JToggleButton;

import java.awt.Component;

import javax.swing.Box;
import javax.swing.JSplitPane;
import javax.swing.JFormattedTextField;
import javax.swing.JPasswordField;
import javax.swing.ImageIcon;

import java.awt.Color;

public class semiChul extends JInternalFrame {

	HashMap<String, MemberData> mem;
	LinkedHashMap<String, ChulGData> chulG ;
	ChulList ChulList = null;
	ProductMain pm;
	ChulGData cgd;

	private JTextField ID;
	private JTextField sPwd;
	private JButton btnNewButton;
	private JButton BtnChul;
	JButton BtnT;
	JButton BtnChulT;
	private JButton logout;
	private JLabel lblNewLabel;
	private JTextField status;

	public semiChul(HashMap<String, MemberData> mem, ProductMain p) {
		super("출퇴근 입력", true, true, true, false);
		getContentPane().setBackground(Color.WHITE);
		this.mem = mem;
		this.pm=p;
		this.chulG=p.chulG;
		setBounds(100, 100, 273, 260);
		getContentPane().setLayout(null);
		getContentPane().add(getID());
		getContentPane().add(getSPwd());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getBtnChul());
		getContentPane().add(getBtnT());
		getContentPane().add(getBtnChulT());
		getContentPane().add(getLogout());
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getStatus());

	}

	private JTextField getID() {
		if (ID == null) {
			ID = new JTextField();
			ID.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					ID.setText("");
				}
			});
			ID.setText("사원번호");
			ID.setColumns(10);
			ID.setBounds(12, 42, 109, 23);
		}
		return ID;
	}

	private JTextField getSPwd() {
		if (sPwd == null) {
			sPwd = new JTextField();
			sPwd.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					sPwd.setText("");
				}
			});
			sPwd.setText("비밀번호");
			sPwd.setColumns(10);
			sPwd.setBounds(12, 75, 109, 23);
		}
		return sPwd;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("Login");
			btnNewButton.setIcon(null);
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try {

						Iterator<String> it = mem.keySet().iterator();

						while(it.hasNext()){

							MemberData md = mem.get(it.next());

							if(md.getCode().equals(ID.getText()) && md.getPwd().equals(sPwd.getText())){								

								BtnChul.setVisible(true);
								BtnT.setVisible(true);
								BtnChulT.setVisible(true);

								status.setText(md.getIrum()+"님, 반갑습니다.");

								lblNewLabel.setText("");
								sPwd.setVisible(false);
								ID.setVisible(false);
								btnNewButton.setVisible(false);

								logout.setVisible(true);

								BtnT.setEnabled(true);
								BtnChul.setEnabled(true);

								return;
							}
						}

						//찾은 데이터가 없을때,
						if(status.getText().trim().equals("")){
							JOptionPane.showMessageDialog(null,	"아이디 또는 비밀번호를 다시 확인하세요.");
						}

					} catch (Exception ex) {
						ex.printStackTrace();
					}
				}
			});
			btnNewButton.setBounds(133, 42, 109, 23);
		}
		return btnNewButton;
	}

	private JButton getBtnChul() {
		if (BtnChul == null) {
			BtnChul = new JButton("출근");
			BtnChul.setVisible(false);
			BtnChul.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd hh: mm: ss");
					String temp = date.format(new Date());
					ChulGData cgd = new ChulGData(ID.getText(), "출근", temp);
					String IDChul = "[" + ID.getText() + "]" + temp; // [A1]2014-05-09 03: 07: 00
					chulG.put(IDChul, cgd);

					System.out.println(IDChul);
					System.out.println(cgd.code);
					System.out.println(cgd.ValueChul);
					System.out.println(temp);
					BtnChul.setEnabled(false);
					BtnT.setEnabled(true);
				}
			});
			BtnChul.setBounds(12, 108, 109, 23);
		}
		return BtnChul;
	}

	private JButton getBtnT() {
		if (BtnT == null) {
			BtnT = new JButton("퇴근");
			BtnT.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd hh: mm: ss");
					String temp = date.format(new Date());
					ChulGData cgd = new ChulGData(ID.getText(), "퇴근", temp);
					String IDChul = "[" + ID.getText() + "]"
							+ temp;
					chulG.put(IDChul, cgd);
					System.out.println(IDChul);
					System.out.println(cgd.code);
					System.out.println(cgd.ValueChul);
					System.out.println(temp);
					BtnT.setEnabled(false);
					BtnChul.setEnabled(true);					
				}
			});
			BtnT.setVisible(false);
			BtnT.setBounds(133, 108, 109, 23);
		}
		return BtnT;
	}
	private JButton getBtnChulT() {
		if (BtnChulT == null) {
			BtnChulT = new JButton("출퇴근목록");
			BtnChulT.setVisible(false);
			BtnChulT.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {

					if (ChulList == null) {
						ChulList = new ChulList(chulG, mem);
						ChulList.setBounds(50, 0, 450, 300);
					}
					pm.contentPane.add(ChulList);
					ChulList.setVisible(true);
				}
			});
			BtnChulT.setBounds(12, 141, 230, 23);
		}
		return BtnChulT;
	}
	private JButton getLogout() {
		if (logout == null) {
			logout = new JButton("Logout");
			logout.setFont(new Font("굴림", Font.PLAIN, 12));
			logout.setVisible(false);
			logout.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {


					BtnChul.setVisible(false);
					BtnT.setVisible(false);
					BtnChulT.setVisible(false);
					status.setText("");
					lblNewLabel.setText("노력은 배신하지 않는다.");
					sPwd.setVisible(true);
					ID.setVisible(true);
					btnNewButton.setVisible(true);
					logout.setVisible(false);



				}
			});
			logout.setBounds(133, 76, 109, 23);
		}
		return logout;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("");
			lblNewLabel.setBounds(12, 108, 230, 23);
		}
		return lblNewLabel;
	}
	private JTextField getStatus() {
		if (status == null) {
			status = new JTextField();
			status.setDisabledTextColor(Color.PINK);
			status.setEditable(false);
			status.setColumns(10);
			status.setBounds(12, 10, 230, 23);
		}
		return status;
	}
}
